$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/projectinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '项目名称', name: 'projectName', index: 'project_name', width: 80 }, 			
			{ label: '招采主体', name: 'companyName', index: 'company_name', width: 80 }, 			
			// { label: '内容详情', name: 'content', index: 'content', width: 80 },
			{ label: '发布时间', name: 'publishedTime', index: 'published_time', width: 80 }, 			
			{ label: '截止时间', name: 'endTime', index: 'end_time', width: 80 }, 			
			{ label: '创建人', name: 'creator', index: 'creator', width: 80 }, 			
			{ label: '修改人', name: 'updator', index: 'updator', width: 80 },
			{ label: '是否可用', name: 'enabled', index: 'enabled', width: 80 , formatter: function (value, options, row) {
                    return value === 0 ?
                        '<span class="label label-success">可用</span>' :
                        '<span class="label label-danger">已删除</span>';
                }
            },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }, 			
			{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $('#summernote').summernote({
        lang: 'zh-CN',
        height: 250,
        width:750,
        tabsize: 2,
        htmlMode: true,
        callbacks: {
            onImageUpload: function (files) {
                //上传图片到服务器
                var formData = new FormData();
                formData.append('file', files[0]);

                $.ajax({
                    url: baseURL + '/upload/image',//后台文件上传接口
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (r) {
                        console.log(JSON.stringify(r));
                        if (r.code == 0)
                            $('#summernote').summernote('insertImage', r.data);
                        else
                            alert("上传失败");
                    }, error: function () {
                        alert("上传失败");
                    }
                });
            }
        }
    });

    $("#datetimepicker_start").datetimepicker({
        forceParse: 0,//设置为0，时间不会跳转1899，会显示当前时间。
        language: 'zh-cn',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: 2,//最精准的时间选择为日期0-分 1-时 2-日 3-月
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: true,//显示今日按钮
        setDate:new Date(),
    });

    $("#datetimepicker_end").datetimepicker({
        forceParse: 0,//设置为0，时间不会跳转1899，会显示当前时间。
        language: 'zh-cn',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: 2,//最精准的时间选择为日期0-分 1-时 2-日 3-月
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: true,//显示今日按钮
        setDate:new Date(),
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		projectInfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.projectInfo = {};
            $('#summernote').summernote('code', '');
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.projectInfo.id == null ? "business/projectinfo/save" : "business/projectinfo/update";
                vm.projectInfo.content = $('#summernote').summernote('code');
                vm.projectInfo.publishedTime = $('#datetimepicker_start').val();
                vm.projectInfo.endTime = $('#datetimepicker_end').val();
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(vm.projectInfo),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "business/projectinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "business/projectinfo/info/"+id, function(r){
                vm.projectInfo = r.projectInfo;
                $('#summernote').summernote('code', vm.projectInfo.content);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});